<?php

if (!defined('IN_XLP')) {
    exit('Access Denied!');
}

/**
 * Description of userNoticeModel
 * 用户消息模型
 * @author xlp
 */
class userNoticeModel extends model {

    function __construct() {
        parent::__construct();
        $this->dbTable = 'user_notice_count';
    }

    /*
     * 添加通知信息
     * 来源类型 0:系统通知 1:通知公告，2:评论/回复，3:提到，4:消息，5:点赞
     * @param $uid int UID
     * @param $ntype int 通知类型
     * @param $counts int 新增消息数量
     * @return int 影响的行数
     */

    public function addNotice($uid, $ntype = 1, $counts = 1) {
        //存在则更新
        if ($this->where(array('uid' => $uid, 'ntype' => $ntype))->find()) {
            return $this->update("counts=counts+" . $counts . ",infotime=" . TIME, array('uid' => $uid, 'ntype' => $ntype));
        } else {//不存在则添加
            return $this->insert(array('uid' => $uid, 'ntype' => $ntype, 'counts' => $counts, 'infotime' => TIME));
        }
    }

    /*
     * 清除通知信息
     * 来源类型 0:系统通知 1:通知公告，2:评论/回复，3:提到，4:消息，5:点赞
     * @param $uid int UID
     * @param $ntype int 通知类型
     * @return int 影响的行数
     */

    public function clearNotice($uid, $ntype = 1) {
        return $this->update(array('counts' => 0, 'infotime' => TIME), array('uid' => $uid, 'ntype' => $ntype));
    }

    /*
     * 获取通知信息
     * @param $uid int UID
     * @return array
     */

    public function getNoticeList($uid) {
        return $this->field('ntype,counts,infotime')->where(array('uid' => $uid))->limit(10)->findAll();
    }

}
